package cn.jjxx.modules.sms.service.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import cn.jjxx.core.common.service.impl.CommonServiceImpl;
import cn.jjxx.core.utils.StringUtils;
import cn.jjxx.modules.sms.entity.SmsSendLog;
import cn.jjxx.modules.sms.mapper.SmsSendLogMapper;
import cn.jjxx.modules.sms.service.ISmsSendLogService;

import org.framework.customutil.sms.wnd.SMSBean;
import org.framework.customutil.sms.wnd.SMSHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**   
 * @Title: 短信发送日志
 * @Description: 短信发送日志
 * @author jeeweb
 * @date 2017-06-08 12:56:37
 * @version V1.0   
 *
 */
@Transactional
@Service("smsSendLogService")
public class SmsSendLogServiceImpl  extends CommonServiceImpl<SmsSendLogMapper,SmsSendLog> implements  ISmsSendLogService {
	
	@Override
	public boolean writeSmsLog(String phones, final String content, final String sequeid) {
		final ISmsSendLogService smsSendLogService = this;
		if(StringUtils.isNotEmpty(phones)&&StringUtils.isNotEmpty(content)){
			List<String> phoneList = Arrays.asList(phones.split(","));
			final SMSHelper smshelper = new SMSHelper();
			for(final String phone:phoneList){
				new Thread(new Runnable() {
					@Override
					public void run() {
						SMSBean bean = smshelper.sendMsg(phone, content, sequeid);
						SmsSendLog sendLog = new SmsSendLog();
						sendLog.setPhone(phone);
						sendLog.setTemplateContent(content);
						if(StringUtils.isNotEmpty(bean.getErrid())){
							sendLog.setStatus(String.valueOf(1));
						}else{
							sendLog.setStatus(String.valueOf(0));
						}
						sendLog.setMsg(bean.getErr());
						sendLog.setCode(bean.getErrid());
						sendLog.setResponseDate(new Date());
						sendLog.setSmsid(sequeid);
						sendLog.setBusinessType(String.valueOf(3));
						sendLog.setSenddata(content);
						smsSendLogService.insert(sendLog);
					}
				}).start();
			}
			return true;
		}
		return false;
	}

}
